<?php

require_once '../../function.php';

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

$size = 20;
$offset = ($page - 1) * $size;

$sql = sprintf("select 
comments.*,
posts.title as post_title,
posts.id as comment_id
from comments
inner join posts on comments.post_id = posts.id
limit %d,%d;",$offset,$size);


$count = (int)xiu_query_one("select
count(1) as count
from comments
inner join posts on comments.post_id = posts.id;")['count'];

$comments = xiu_query_all($sql);    //字符串


//最大页数
$total_page = (int)ceil($count / $size);

$json = json_encode(array(
    'total_page' => $total_page,
    'comment' => $comments,
));

header('Content-Type:application/json');
echo $json;



